<?php

namespace App\Services\ApplyGoldCoinWithdraw;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Withdraw;
use App\Models\GoldRound;
use App\Http\Controllers\Api\V1\Requests\FormRequest;

class ApplyGoldCoinWithdrawRequest extends FormRequest
{
	public function rules(): array
	{
		/**
		 * @var User $user
		 */
		$user = $this->user();

		$time = Carbon::now();

		if ($time->lt(Carbon::today()->hour(9)->minute(30))
			or $time->gt(Carbon::today()->hour(21)->minute(30))) {
			fail('每日提现时间:09:30:00~21:30:00');
		} elseif (! $user->isAlipayBound()) {
			fail('请先绑定支付宝账号');
		} elseif (! $user->cert_number) {
			fail('请先绑定银行卡账号');
		} elseif (! $user->pay_code) {
			fail('请先设置交易密码');
		} elseif (! $this->isStormSupportMatched()) {
			fail('扶持用户100元以上部分可提现');
		}/* elseif ($user->isWithdrawDanger($this->coins)) {
			fail('资金安全指数较低请联系客服确认');
		}*/ elseif (GoldRound::hasAnyPeak()) {
			fail('请在抢购高峰后申请提现');
		} elseif ($user->hasAppliedWithdrawToday()) {
			fail('每日仅可提现一次');
		}

		return [
			'coins' => [
				'required',
				'numeric',
				'min:1',
				$this->coins()
			],
			'pay_code' => $this->paycode(false)
		];
	}

	public function attributes()
	{
		return [
			'coins' => '金额',
			'pay_code' => '交易密码',
		];
	}

	public function coins()
	{
		return function ($key, $value, $fail) {
			if ($this->isFloatInvalid($value)) {
				return $fail('请输入正确的提现金额');
			} elseif ($this->user()->gold_coins <= 0) {
				return $fail('可提现余额不足');
			} elseif (! $this->isWithdrawOn()) {
				return $fail('平台尚未开放提现功能');
			} elseif (! $this->isStormSupportMatched()) {
				return $fail('扶持用户100元以上部分可提现');
			} elseif (! $this->isLowestAmountMatched()) {
				return $fail("提现金额不能少于 '{$this->allowedLowestAmount()}' 元");
			}  elseif (! $this->isHighestAmountMatched()) {
				return $fail("提现金额不能超过 '{$this->allowedHighestAmount()}' 元");
			}  elseif (! $this->isGoldCoinsEnough()) {
				return $fail('可提现余额不足');
			}/* elseif (! $this->isAlipayMoneyEnough()) {
				return $fail('提现资金正在入账请稍后再试');
			}*/ else {
				return true;
			}
		};
	}

	protected function isWithdrawOn(): bool
	{
		return Withdraw::isWithdrawOn();
	}

	protected function isStormSupportMatched(): bool
	{
        $supportCoins = cs('storm')->gold_support_coins;

        if ($supportCoins <= 0) return true;

		/**
		 * @var User $user
		 */
		$user = $this->user();

		$hasSupported = $user->goldCoins()->where('remark', 'support_gold_coins')->exists();

		if (!$hasSupported) return true;

		$isLeftCoinsMatched = big_decimal($user->gold_coins)->minus($this->coins)->isGreaterThanOrEqualTo($supportCoins);

		if ($isLeftCoinsMatched) return true;

		return false;
	}

	protected function allowedLowestAmount()
	{
		return Withdraw::allowedLowestAmount();
	}

	protected function isLowestAmountMatched(): bool
	{
		return Withdraw::isLowestAmountMatched($this->coins);
	}

	protected function allowedHighestAmount()
	{
		return Withdraw::allowedHighestAmount();
	}

	protected function isHighestAmountMatched(): bool
	{
		return Withdraw::isHighestAmountMatched($this->coins);
	}

	protected function isGoldCoinsEnough(): bool
	{
		return Withdraw::isCoinsEnoughToDeduct($this->user(), $this->coins);
	}

	protected function isAlipayMoneyEnough(): bool
	{
		$actualAmount = Withdraw::willActuallyGetCoins($this->coins);
		$activeFund = cashier('alibatch')->findActive();

		if (!is_numeric($activeFund)) return false;

		return big_decimal($activeFund)->isGreaterThan($actualAmount);
	}
}
